package cn.coderdream.tools;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CookieUtils {
    public static List<String> getCookie(String address) {
        URL url = null;
        List<String> cookieList = new ArrayList<>();
        try {
            url = new URL(address);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.connect();
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                Map<String, List<String>> headFields = conn.getHeaderFields();
                List<String> list = headFields.get("Set-Cookie");
                if (list != null) {
                    cookieList.addAll(list);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (cookieList.size() > 0) {
            return cookieList;
        } else {
            return null;
        }
    }
}
